//
//  HMsecondCell.m
//  学习的时长
//
//  Created by 李贺 on 2017/1/8.
//  Copyright © 2017年 李贺. All rights reserved.
//

#import "HMsecondCell.h"
#import <Masonry.h>
#import "PNChart.h"
#import "UILabel+Addition.h"
@interface HMsecondCell ()<PNChartDelegate>
@property (nonatomic, strong) PNBarChart *barChart;
@end
@implementation HMsecondCell

// 重写父类的初始化方法,在此方法中添加cell中的子控件
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // 添加子控件
        [self setupUI];
    }
    return self;
}

#pragma mark - 添加cell中的子控件

- (void)setupUI {
    UIView *histogramView = [[UIView alloc] initWithFrame:self.contentView.bounds];
    [self.contentView addSubview:histogramView];
    
    [histogramView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.contentView);
    }];
    //柱状图
    
    static NSNumberFormatter *barChartFormatter;
    if (!barChartFormatter){
        barChartFormatter = [[NSNumberFormatter alloc] init];
        barChartFormatter.numberStyle = kCFNumberFormatterNoStyle;
        barChartFormatter.allowsFloats = NO;
        barChartFormatter.maximumFractionDigits = 0;
    }
    
    PNBarChart *barChart = [[PNBarChart alloc] initWithFrame:CGRectMake(0, 80, SCREEN_WIDTH, 200.0)];
    
    self.barChart = barChart;
    
    barChart.backgroundColor = [UIColor clearColor];
    barChart.yLabelFormatter = ^(CGFloat yValue){
        return [barChartFormatter stringFromNumber:[NSNumber numberWithFloat:yValue]];
    };
    
    barChart.yChartLabelWidth = 30.0;
    barChart.chartMarginLeft = 40.0;
    barChart.chartMarginRight = 10.0;
    barChart.chartMarginTop = 5.0;
    barChart.chartMarginBottom = 10.0;
    
    barChart.labelMarginTop = 1.0;
    barChart.showChartBorder = YES;
    
    NSDate *date = [NSDate date];
    
    NSCalendar *calender = [NSCalendar currentCalendar];
    
    NSTimeZone *zone = [NSTimeZone localTimeZone];
    
    [calender setTimeZone:zone];
    NSInteger mouth = [calender component:NSCalendarUnitMonth fromDate:date];
    NSInteger day = [calender component:NSCalendarUnitDay fromDate:date];
    NSString *dayStr7 = [NSString stringWithFormat:@"%zd.%zd",mouth,day];
    NSString *dayStr6 = [NSString stringWithFormat:@"%zd.%zd",mouth,day-1];
    NSString *dayStr5 = [NSString stringWithFormat:@"%zd.%zd",mouth,day-2];
    NSString *dayStr4 = [NSString stringWithFormat:@"%zd.%zd",mouth,day-3];
    NSString *dayStr3 = [NSString stringWithFormat:@"%zd.%zd",mouth,day-4];
    NSString *dayStr2 = [NSString stringWithFormat:@"%zd.%zd",mouth,day-5];
    NSString *dayStr1 = [NSString stringWithFormat:@"%zd.%zd",mouth,day-6];
    
    [barChart setXLabels:@[dayStr1,dayStr2,dayStr3,dayStr4,dayStr5,dayStr6,dayStr7]];
    
    [barChart setYValues:@[@10,@150,@67,@33,@10,@88,@139]];
    
    
    barChart.strokeColor = [UIColor colorWithRed:30/255.0 green:144/255.0 blue:255/255.0 alpha:0.7];
    barChart.isGradientShow = NO;
    barChart.isShowNumbers = YES;
    [barChart strokeChart];
    
    barChart.delegate = self;
    
    [histogramView addSubview:barChart];
    
    UIImageView *titleImage = [[UIImageView alloc] init];
    [histogramView addSubview:titleImage];
    titleImage.image = [UIImage imageNamed:@"audio_icon"];
    
    [titleImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(histogramView.mas_left).offset(16);
        make.top.equalTo(histogramView.mas_top).offset(16);
    }];
    
    UILabel *titleLable = [UILabel hm_labelWithText:@"每日学习时间" fontSize:k_FONT_BIG color:[UIColor blackColor]];
    [histogramView addSubview:titleLable];
    
    [titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleImage);
        make.left.equalTo(titleImage.mas_right).offset(8);
    }];
    
    UILabel *detailLable = [UILabel hm_labelWithText:@"提示:学习分钟/日期" fontSize:k_FONT_NORMAL color:[UIColor darkGrayColor]];
    
    [histogramView addSubview:detailLable];
    
    [detailLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLable.mas_bottom).offset(16);
        make.left.equalTo(titleLable);
    }];
}


- (void)userClickedOnBarAtIndex:(NSInteger)barIndex
{
    PNBar * bar = [self.barChart.bars objectAtIndex:barIndex];

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];

    animation.fromValue = @1.0;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.toValue = @1.1;
    animation.duration = 0.2;
    animation.repeatCount = 0;
    animation.autoreverses = YES;
    animation.removedOnCompletion = YES;
    animation.fillMode = kCAFillModeForwards;

    [bar.layer addAnimation:animation forKey:@"Float"];
}







@end











